package com.example.vue.comm.util;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * web 相关的工具类
 *
 * @author zhouxl
 * @DATE 2020/11/26
 * @descriptoin
 */
public class WebUtil {

    /**
     * 获取浏览器 userAgent信息
     *
     * @param request
     * @return
     */
    public static Map<String, Object> getUserAgentInfo(HttpServletRequest request) {
        Map<String, Object> agentInfoMap = new HashMap<>();
        String browserDetails = request.getHeader("User-Agent");
        String userAgent = browserDetails;
        String user = userAgent.toLowerCase();

        String os = "";
        String browser = "";

        //=================OS Info=======================
        if (userAgent.toLowerCase().indexOf("windows") >= 0) {
            os = "Windows";
        } else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
            os = "Mac";
        } else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
            os = "Unix";
        } else if (userAgent.toLowerCase().indexOf("android") >= 0) {
            os = "Android";
        } else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
            os = "IPhone";
        } else {
            os = "UnKnown, More-Info: " + userAgent;
        }
        //===============Browser===========================
        if (user.contains("edge")) {
            browser = (userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("msie")) {
            String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
            browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
        } else if (user.contains("safari") && user.contains("version")) {
            browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]
                    + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
        } else if (user.contains("opr") || user.contains("opera")) {
            if (user.contains("opera")) {
                browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]
                        + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
            } else if (user.contains("opr")) {
                browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))
                        .replace("OPR", "Opera");
            }

        } else if (user.contains("chrome")) {
            browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
        } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) ||
                (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) ||
                (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
            browser = "Netscape-?";
        } else if (user.contains("firefox")) {
            browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("rv")) {
            String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");
            browser = "IE" + IEVersion.substring(0, IEVersion.length() - 1);
        } else {
            browser = "UnKnown, More-Info: " + userAgent;
        }

        agentInfoMap.put("os", os);
        agentInfoMap.put("browser", browser);
//        return os +" --- "+ browser ;
        return agentInfoMap;
    }


    /**
     * 将请求发方式转换 对应的数字编号
     * get post delete put
     *
     * @return
     */
    public static int methodToReqTypeParams(String method) {
        if (StringUtil.isEmpty(method)) {    // 错误异常
            return 0;
        }
        method = method.toLowerCase();
        switch (method) {
            case "get":
                return 1;
            case "post":
                return 2;
            case "delete":
                return 3;
            case "put":
                return 4;
            default:
                return 0;
        }
    }

    /**
     * 请求参数的uri转换成查询参数的reqUrl信息
     * 例子： /api/system/test -> api/system/test
     *
     * @return
     */
    public static String uriToReqUrlParams(String uri) {
        String[] split = uri.split("/");
        return String.join("/", split);
    }

    /**
     * 转换url信息，变成code信息
     * 例子：
     * /api/system/list --> api:system:list
     *
     * @return
     */
    public static String convertUrlToCode(String url) {
        if (Objects.isNull(url)) {
            return null;
        }
        if ("".equals(url.trim())) {
            return "";
        }
        String[] split = url.split("/");
        return String.join(":", split);
    }

    /**
     * 获取客户端ip地址信息
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader(" x-forwarded-for ");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 判断当前请求是否是jsonp请求
     * true  - 是jsonp请求
     * false - 非jsonp请求
     *
     * @param request HttpServletRequest
     * @return
     */
    public static boolean isJsonpRequest(HttpServletRequest request) {
        String callbackparam = request.getParameter("callbackparam");
        if (callbackparam != null &&
                "jsonpCallback".toLowerCase().equals(callbackparam)) {
            return true;
        }
        return false;
    }

    /**
     * 判断当前是否是ajax请求
     * true  - 是ajax请求
     * false - 非ajax请求
     *
     * @param request HttpServletRequest
     * @return
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestType = request.getHeader("x-requested-with");
        if (requestType == null) {
            return false;
        }
        return "XMLHttpRequest".toLowerCase().equals(requestType.trim().toLowerCase());
    }

}
